package com.invite.me.ui.yaoyue;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.invite.me.R;
import com.invite.me.base.BaseActivity;
import com.invite.me.beans.presenter.InviteRecordDetailByPresenter;
import com.invite.me.beans.res.InviteRecordDetailByXqShouBean;
import com.invite.me.beans.res.InviteRecordTyfuShouBean;
import com.invite.me.util.ToastUtils;
import com.invite.me.wlqq.Contract;

public class InviteRecordDetailByyActivity  extends BaseActivity implements View.OnClickListener,Contract.BaseView{

    //被邀约人
    InviteRecordDetailByPresenter inviteRecordDetailByPresenter;
    private RelativeLayout fuyuezhong;
    private RelativeLayout yifuyue;
    private RelativeLayout yiwancheng;




    private String xiangqingid;
    private String xiangqingzijiid;
    private LinearLayout tongyifuyuee;
    private TextView tongyifuyuebutton;
    private LinearLayout quxiaoyaoyue;
    private TextView quxiaoyaoyuebutton;
    private ImageView image_fanhui;

    @Override
    protected void initView() {
        inviteRecordDetailByPresenter=new InviteRecordDetailByPresenter(this);
        Intent intent = getIntent();
        //  String leixing = intent.getStringExtra("state");
        String qianyiyeid = intent.getStringExtra("id");
        inviteRecordDetailByPresenter.ChakanXiangqing(qianyiyeid);
        image_fanhui = findViewById(R.id.image_fanhui);

        //同意赴约
        tongyifuyuee = findViewById(R.id.tongyifuyuee);

        //取消邀约
        quxiaoyaoyue = findViewById(R.id.quxiaoyaoyue);
        quxiaoyaoyuebutton = findViewById(R.id.quxiaoyaoyuebutton);
        tongyifuyuebutton = findViewById(R.id.tongyifuyuebutton);



        tongyifuyuebutton.setOnClickListener(this);
        quxiaoyaoyuebutton.setOnClickListener(this);
        image_fanhui.setOnClickListener(this);




    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.image_fanhui:
                finish();
                break;


            case R.id.tongyifuyuebutton:
                tongyifupop(xiangqingid,xiangqingzijiid);
                break;
            case R.id.quxiaoyaoyuebutton:

                break;

        }

    }
    @Override
    protected int getActivityLayoutId() {
        return R.layout.activity_inviterecorddetail_byy;
    }

    @Override
    protected void loadData() {

    }


    @Override
    public void setPresenter(Contract.BasePresenter basePresenter) {
        if (inviteRecordDetailByPresenter instanceof Contract.BasePresenter) {
            this.inviteRecordDetailByPresenter = (InviteRecordDetailByPresenter) basePresenter;
        }
    }

    @Override
    public void onDataSucess(Object o){
        if(o instanceof InviteRecordDetailByXqShouBean){
            InviteRecordDetailByXqShouBean inviteRecordDetailByXqShouBean= (InviteRecordDetailByXqShouBean) o;
            if(inviteRecordDetailByXqShouBean.getCode()==1000){
                double value = inviteRecordDetailByXqShouBean.getData().getValue();
                String address = inviteRecordDetailByXqShouBean.getData().getAddress();
                xiangqingid = inviteRecordDetailByXqShouBean.getData().getId();
                xiangqingzijiid = inviteRecordDetailByXqShouBean.getData().getUserIdb();

                //0:未支付,1:邀约接受者已确认,2:已支付,3:已完成,4:已支付佣金,50:已退款,100:已取消,101:邀约发起者已取消,102:邀约接受者已取消")
                String state = inviteRecordDetailByXqShouBean.getData().getState();
                if(!TextUtils.isEmpty(state)){
                    if(state.equals("0")){
                        //未支付
                        tongyifuyuee.setVisibility(View.VISIBLE);


                    }else if(state.equals("1")){
                        //要约接受着已确认
                        quxiaoyaoyue.setVisibility(View.VISIBLE);

                    }else if(state.equals("2")){
                        //已支付


                    }else if(state.equals("3")){
                        //已完成

                    }else if(state.equals("4")){
                        //已支付佣金

                    }else if(state.equals("50")){
                        //已退款
                    }else if(state.equals("100")){
                        //已取消
                    }else if(state.equals("101")){
                        //邀约者已取消
                    }else if(state.equals("102")){
                        //接受者已取消
                    }



                }




            }


        }else if(o instanceof InviteRecordTyfuShouBean){
            InviteRecordTyfuShouBean inviteRecordTyfuShouBean= (InviteRecordTyfuShouBean) o;
            if(inviteRecordTyfuShouBean.getCode()==10000){
                ToastUtils.show(InviteRecordDetailByyActivity.this,inviteRecordTyfuShouBean.getMessage());

            }


        }


    }

    @Override
    public void onDataFaile(Object o) {

    }

    @Override
    public void onComplete() {

    }
    public void tongyifupop(final String zhifuid, final String zijiid){

        View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        View view = LayoutInflater.from(this).inflate(R.layout.tongyifuyue_pop, null, false);
        TextView  queding=view.findViewById(R.id.queding);

        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setFocusable(true);
        //产生背景变暗效果
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
        popupWindow.setOutsideTouchable(true);// 设置同意在外点击消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inviteRecordDetailByPresenter.Tongyiyaoyue(zhifuid,zijiid);
                popupWindow.dismiss();

            }
        });




        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            //在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });

        popupWindow.showAtLocation(parent, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);



    }
    //取消邀约
    public void yaoyuepop() {
        View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        View view = LayoutInflater.from(this).inflate(R.layout.beiyaoyuequxiao_pop, null, false);
        TextView quxiao=view.findViewById(R.id.quxiao);
        TextView queding=view.findViewById(R.id.queding);


        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setFocusable(true);
        //产生背景变暗效果
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
        popupWindow.setOutsideTouchable(true);// 设置同意在外点击消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);

        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



            }
        });
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });




        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            //在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });

        popupWindow.showAtLocation(parent, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);


    }

}